package com.ydj.framework.third.screw;

import cn.hutool.core.io.FileUtil;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.ydj.AppEnvironment;
import com.ydj.framework.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.File;
import java.util.ArrayList;

@Lazy
@Component
@Slf4j
public class ScrewTools {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private AppEnvironment appEnvironment;

    public synchronized String make() throws CommonException {
        if (appEnvironment.isProd()) {
            return "不支持该功能";
        }
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);

        File tempFile = new File(appEnvironment.getJavaTempDirectory(), "dict.html");
        try {
            // 生成文件配置
            EngineConfig engineConfig = EngineConfig.builder()
                    // 生成文件路径，自己mac本地的地址，这里需要自己更换下路径
                    .fileOutputDir(tempFile.getParent())
                    .fileName(tempFile.getName().replace(".html", ""))
                    // 打开目录
                    .openOutputDir(false)
                    // 文件类型
                    .fileType(EngineFileType.HTML)
                    // 生成模板实现
                    .produceType(EngineTemplateType.freemarker).build();

            // 生成文档配置（包含以下自定义版本号、描述等配置连接）
            Configuration config = Configuration.builder()
                    .version("1.0.0")
                    .description("数据字典")
                    .dataSource(dataSourceMysql)
                    .engineConfig(engineConfig)
                    .produceConfig(getProcessConfig())
                    .build();

            // 执行生成
            new DocumentationExecute(config).execute();
            return FileUtil.readString(tempFile, "utf-8");

        } catch (Exception e) {
            log.error("出错了，原因是" + e.getMessage(), e);
            CommonException.throwException("出错了，原因是" + e.getMessage());
        } finally {
            if (tempFile.exists()) {
                tempFile.delete();
            }
        }
        return "出错了";
    }


    /**
     * 配置想要生成的表+ 配置想要忽略的表
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig(){
        // 忽略表名
//        List<String> ignoreTableName = Arrays.asList("aa","test_group");
        // 忽略表前缀，如忽略a开头的数据库表
//        List<String> ignorePrefix = Arrays.asList("a","t");
        // 忽略表后缀
//        List<String> ignoreSuffix = Arrays.asList("_test","czb_");

        return ProcessConfig.builder()
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
//                .ignoreTableName(ignoreTableName)
                //忽略表前缀
//                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
//                .ignoreTableSuffix(ignoreSuffix)
                .build();
    }
}
